<template>
  <div class="search-history-container">
    <van-cell-group>
      <van-cell title="历史记录" center>
        <div v-if="isShowDelete" class="opt-btn">
          <span @click="$emit('delete', -1)">全部删除</span>
          <span @click="isShowDelete = false">完成</span>
        </div>
        <van-icon name="delete" v-else @click="isShowDelete = true" />
      </van-cell>
      <van-cell
        v-for="(item, index) in historyList"
        :key="index"
        :title="item"
        @click="handleHistoryItem(item, index)"
      >
        <van-icon v-show="isShowDelete" name="close" />
      </van-cell>
    </van-cell-group>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  props: {
    historyList: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      isShowDelete: false
    }
  },
  methods: {
    // cat 操作分类 0：跳转，1：删除
    // item 点击项
    // index 数组标志位
    handleHistoryItem(item, index) {
      // 删除状态下
      if (this.isShowDelete) {
        this.$emit('delete', index)
      } else {
        // 非删除状态下
        this.$emit('search-suggestion', item)
      }
    }
  }
}
</script>
<style lang="less" scoped>
.opt-btn {
  span:nth-child(1) {
    margin-right: 10px;
  }
}
</style>
